<script setup lang="ts">
import { ref } from "vue";
import { useNav } from "@/layout/hooks/useNav";
import { responsiveStorageNameSpace } from "@/config";
import { storageLocal } from "@pureadmin/utils";
import { getTopMenu } from "@/router/utils";
import { useCustomerServiceStore } from "@/store/modules/customerService";
import { emitter } from "@/utils/mitt";
import { DataAnalysis, ChatDotRound, User, Setting } from "@element-plus/icons-vue";
import { onMounted, onBeforeUnmount } from "vue";
import LaySidebarLeftCollapse from "@/layout/components/lay-sidebar/components/SidebarLeftCollapse.vue";

const store = useCustomerServiceStore();
const isShow = ref(false);
const showLogo = ref(
  storageLocal().getItem<StorageConfigs>(`${responsiveStorageNameSpace()}configure`)?.showLogo ?? true
);

const { device, pureApp, isCollapse, tooltipEffect, toggleSideBar } = useNav();

onMounted(() => {
  emitter.on("logoChange", key => {
    showLogo.value = key;
  });
});

onBeforeUnmount(() => {
  emitter.off("logoChange");
});
</script>

<template>
  <div
    :class="['sidebar-container', showLogo ? 'has-logo' : 'no-logo']"
    @mouseenter.prevent="isShow = true"
    @mouseleave.prevent="isShow = false"
  >
    <!-- Logo区域 -->
    <div v-if="showLogo" class="sidebar-logo-container" :class="{ collapse: isCollapse }">
      <transition name="sidebarLogoFade">
        <router-link
          v-if="!isCollapse"
          key="expand"
          class="sidebar-logo-link"
          :to="getTopMenu()?.path ?? '/'"
        >
          <el-icon class="sidebar-logo-icon"><User /></el-icon>
          <h1 class="sidebar-title">客户服务</h1>
        </router-link>
        <router-link v-else key="collapse" class="sidebar-logo-link" :to="getTopMenu()?.path ?? '/'">
          <el-icon class="sidebar-logo-icon"><User /></el-icon>
        </router-link>
      </transition>
    </div>

    <!-- 菜单区域 -->
    <el-scrollbar wrap-class="scrollbar-wrapper" :class="[device === 'mobile' ? 'mobile' : 'pc']">
      <el-menu
        router
        unique-opened
        mode="vertical"
        popper-class="pure-scrollbar"
        class="outer-most select-none"
        :default-active="$route.path"
        :collapse="isCollapse"
        :collapse-transition="false"
        :popper-effect="tooltipEffect"
      >
        <el-menu-item index="/customer-service/dashboard">
          <el-icon><DataAnalysis /></el-icon>
          <template #title>方舟面板</template>
        </el-menu-item>
        <el-menu-item index="/customer-service/inbox">
          <el-icon><ChatDotRound /></el-icon>
          <template #title>
            <span>收件箱</span>
            <el-badge v-if="store.newRequestCount > 0" :value="store.newRequestCount" class="menu-badge" />
          </template>
        </el-menu-item>
        <el-menu-item index="/customer-service/rooms">
          <el-icon><User /></el-icon>
          <template #title>聊天室列表</template>
        </el-menu-item>
        <el-menu-item index="/customer-service/settings">
          <el-icon><Setting /></el-icon>
          <template #title>设置</template>
        </el-menu-item>
      </el-menu>
    </el-scrollbar>

    <!-- 折叠按钮 -->
    <LaySidebarLeftCollapse
      v-if="device !== 'mobile'"
      :is-active="pureApp.sidebar.opened"
      @toggleClick="toggleSideBar"
    />
  </div>
</template>

<style lang="scss" scoped>
.sidebar-container {
  background: #273142;

  // Logo区域样式
  .sidebar-logo-container {
    position: relative;
    width: 100%;
    height: 48px;
    padding: 10px 0;
    overflow: hidden;
    line-height: 48px;
    text-align: center;

    & .sidebar-logo-link {
      display: flex;
      gap: 12px;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;

      & .sidebar-logo-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        font-size: 18px;
        color: #fff;
        background: #409eff;
        border-radius: 8px;
      }

      & .sidebar-title {
        margin: 0;
        font-size: 16px;
        font-weight: 600;
        color: #000;
        white-space: nowrap;
      }
    }

    &.collapse {
      .sidebar-logo-link {
        gap: 0;
        justify-content: center;
      }
    }
  }

  // 菜单项样式
  :deep(.el-menu) {
    padding: 8px;
    background: transparent;
    border: none;

    .el-menu-item {
      height: 44px;
      margin-bottom: 4px;
      line-height: 44px;
      color: #a8b1bd;
      border-radius: 8px;

      &:hover {
        color: #fff;
        background: rgb(255 255 255 / 8%);
      }

      &.is-active {
        color: #fff;
        background: #409eff;
      }

      .el-icon {
        margin-right: 12px;
        font-size: 18px;
      }

      .menu-badge {
        margin-left: auto;
      }
    }
  }
}

// Logo过渡动画
.sidebarLogoFade-enter-active {
  transition: opacity 1.5s;
}

.sidebarLogoFade-enter,
.sidebarLogoFade-leave-to {
  opacity: 0;
}
</style>
